package com.xbongbong.saas.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormExplainEntity;
import com.xbongbong.paas.domain.entity.PaasMenuEntity;
import com.xbongbong.paas.domain.entity.PaasProcessDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.elasticsearch.pojo.XbbAggregatedPage;
import com.xbongbong.paas.elasticsearch.util.EsUtil;
import com.xbongbong.paas.elasticsearch.util.XbbElasticsearchRestTemplate;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.FlowStatusEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.help.CommonHelp;
import com.xbongbong.sys.help.PackageHelp;
import com.xbongbong.paas.model.PaasFormExplainModel;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.model.PaasMenuModel;
import com.xbongbong.paas.model.PaasProcessDataModel;
import com.xbongbong.paas.pojo.ContactLinkCustomerPojo;
import com.xbongbong.paas.pojo.FormTitlePoJo;
import com.xbongbong.paas.pojo.PhoneSubformPojo;
import com.xbongbong.paas.pojo.SummaryDataPoJo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.pro.common.pojo.CheckRepeatToolHeadPojo;
import com.xbongbong.pro.common.pojo.CheckRepeatToolPojo;
import com.xbongbong.pro.common.pojo.ReCheckSearchResultPojo;
import com.xbongbong.pro.common.pojo.RepeatApprovalPojo;
import com.xbongbong.pro.common.pojo.RepeatSearchFieldPojo;
import com.xbongbong.pro.common.pojo.SeniorCheckRepeatSearchPojo;
import com.xbongbong.pro.common.pojo.dto.CheckRepeatToolDTO;
import com.xbongbong.pro.common.pojo.dto.MobileCheckRepeatToolDTO;
import com.xbongbong.pro.common.pojo.dto.SeniorReCheckSearchDTO;
import com.xbongbong.pro.common.pojo.vo.CheckRepeatToolVO;
import com.xbongbong.pro.constant.ElasticConstant;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.RecheckingErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.recheck.pojo.RecheckAttrRulePojo;
import com.xbongbong.pro.recheck.pojo.RecheckFormRulePojo;
import com.xbongbong.pro.weblist.pojo.RepeatFieldPojo;
import com.xbongbong.pro.weblist.pojo.dto.RepeatToolFieldGetDTO;
import com.xbongbong.pro.weblist.pojo.vo.RepeatFieldListVO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.entity.RecheckingRuleEntity;
import com.xbongbong.saas.enums.RecheckMatchWayEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ClueEnum;
import com.xbongbong.saas.enums.business.ContactEnum;
import com.xbongbong.saas.enums.business.CustomerManagementEnum;
import com.xbongbong.saas.enums.business.SalesOpportunityEnum;
import com.xbongbong.saas.factory.business.RecheckSearchFactory;
import com.xbongbong.saas.help.RecheckingRuleHelp;
import com.xbongbong.saas.model.IndexTypeModel;
import com.xbongbong.saas.model.RecheckingRuleModel;
import com.xbongbong.saas.service.RecheckingService;
import com.xbongbong.saas.service.recheck.abstracts.RecheckSearchStrategy;
import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.join.query.HasChildQueryBuilder;
import org.elasticsearch.join.query.JoinQueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import static com.xbongbong.paas.elasticsearch.constant.EsNormalConstant.MINI_MUM_SHOULD_MATCH;
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.index.query.QueryBuilders.termsQuery;

/**
 * 重复规则相关实现
 * @author lijiangren
 * @date 2022-11-15 09 17
 */
@Service("recheckingService")
public class RecheckingServiceImpl implements RecheckingService {
    private static final Logger LOG = LoggerFactory.getLogger(RecheckingServiceImpl.class);
    @Resource
    private RecheckingRuleModel recheckingRuleModel;
    @Resource
    private PaasFormExplainModel paasFormExplainModel;
    @Resource
    private PaasFormModel paasFormModel;
    @Resource
    private RecheckingRuleHelp recheckingRuleHelp;
    @Resource
    private RecheckSearchFactory recheckSearchFactory;
    @Resource
    private CommonHelp commonHelp;
    @Resource
    private PackageHelp packageHelp;
    @Resource
    private PaasProcessDataModel paasProcessDataModel;
    @Resource
    private IndexTypeModel indexTypeModel;
    @Resource
    private XbbElasticsearchRestTemplate xbbElasticsearchRestTemplate;
    @Resource
    private PaasMenuModel paasMenuModel;


    /**
     * 常用查重搜索
     *
     * @param checkRepeatToolDTO
     * @throws
     * @return
     * @author lijiangren
     * @date 2022-11-15 09 18
     * @since
     * @version
     */
    @Override
    public CheckRepeatToolVO searchCommonRecheck(CheckRepeatToolDTO checkRepeatToolDTO) throws XbbException {
        CheckRepeatToolVO checkRepeatToolVO = new CheckRepeatToolVO();

        try {
            String corpid = checkRepeatToolDTO.getCorpid();
            Integer businessType = checkRepeatToolDTO.getBusinessType();
            String checkValue = checkRepeatToolDTO.getCheckValue();
            if (Objects.isNull(checkValue)) {
                throw new XbbException(RecheckingErrorCodeEnum.API_ERROR_330001);
            }
            //公司是否开启了工作流
            boolean isOpenWorkflow = commonHelp.isOpenWorkFlow(corpid);
            checkRepeatToolDTO.setIsOpenWorkflow(isOpenWorkflow);
            Integer feeType = packageHelp.getFeeType(corpid);
            checkRepeatToolDTO.setFeeType(feeType);
            RecheckSearchStrategy recheckSearchStrategy = recheckSearchFactory.getRecheckSearchStrategy(businessType);
            //查重启用的表单
            List<PaasFormEntityExt> enableFormList = paasFormModel.getFormListDistributor(corpid, businessType, BasicConstant.ZERO);
            List<Long> aggDataForm = getCommonRepeatAggDataFormId(checkRepeatToolDTO,enableFormList);
            //查审批数据
            List<PaasProcessDataEntity> paasProcessDataEntityList = new ArrayList<>();
            if (recheckingRuleHelp.checkSearchApproval(checkRepeatToolDTO)) {
                RepeatApprovalPojo repeatApprovalPojo = new RepeatApprovalPojo(checkRepeatToolDTO.getCheckValue(),businessType, corpid);
                paasProcessDataEntityList = getInApprovalData(null,RecheckMatchWayEnum.FUZZY_MATCH.getCode(), BasicConstant.ONE, aggDataForm,repeatApprovalPojo);
            }
            //移除未启用表单模版查出来的脏数据
            List<Long> formIds = enableFormList.stream().map(PaasFormEntityExt::getId).collect(Collectors.toList());
            aggDataForm.retainAll(formIds);
            if (CollectionsUtil.isNotEmpty(aggDataForm)) {
                //没有权限
                PaasFormEntityExt paasFormEntityExt= enableFormList.stream().filter(item -> Objects.equals(item.getId(), aggDataForm.get(0))).findAny().orElse(null);
                PaasMenuEntity paasMenuEntity = paasMenuModel.getByKey(paasFormEntityExt.getMenuId(), corpid);

                if (checkRepeatToolDTO.getIsClueLinkContact() || checkRepeatToolDTO.getIsClueLinkCustomer()) {
                    boolean permissionFlag = recheckingRuleHelp.checkMenuPermission(businessType, checkRepeatToolDTO.getLoginUser());
                    if (!permissionFlag) {
                        checkRepeatToolVO.setMenuName(paasMenuEntity.getName());
                        checkRepeatToolVO.setBusinessType(businessType);
                        checkRepeatToolVO.setNoPermission(1);
                        checkRepeatToolVO.setCheckList(new ArrayList<>());
                        return checkRepeatToolVO;
                    }
                }
                Long firstFormId = aggDataForm.get(0);
                //返回高亮字段
                List<RepeatSearchFieldPojo> repeatSearchFieldPojos = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                //返回字段定死电话和名称
                repeatSearchFieldPojos.add(new RepeatSearchFieldPojo(firstFormId, StringConstant.NAME, checkRepeatToolDTO.getCheckValue()));
                repeatSearchFieldPojos.add(new RepeatSearchFieldPojo(firstFormId, StringConstant.PHONE, checkRepeatToolDTO.getCheckValue()));
                //查询ES中的数据
                XbbAggregatedPage<PaasFormDataEntityExt> esEntities = getCommonDuplicateData(aggDataForm, checkRepeatToolDTO);
                //判断是否有审批,查重当前表单的审批数据
                List<PaasProcessDataEntity> partPaasProcessData = new ArrayList<>();
                if (CollectionsUtil.isNotEmpty(paasProcessDataEntityList)) {
                    partPaasProcessData = paasProcessDataEntityList.stream().filter(item -> Objects.equals(firstFormId, item.getFormId())).collect(Collectors.toList());
                }
                if (CollectionsUtil.isNotEmpty(esEntities.getContent()) || CollectionsUtil.isNotEmpty(partPaasProcessData)) {

                    List<CheckRepeatToolPojo> checkRepeatToolPojoList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                    ReCheckSearchResultPojo reCheckSearchResultPojo = new ReCheckSearchResultPojo();
                    reCheckSearchResultPojo.setFirstFormId(firstFormId);
                    reCheckSearchResultPojo.setPaasFormDataEntityList(esEntities.getContent());
                    reCheckSearchResultPojo.setPartPaasProcessData(partPaasProcessData);
                    reCheckSearchResultPojo.setRepeatSearchFieldList(repeatSearchFieldPojos);
                    reCheckSearchResultPojo.setPaasFormEntityExt(paasFormEntityExt);
                    //查重结果集策略
                    CheckRepeatToolPojo checkRepeatToolPojo = recheckSearchStrategy.repeatResult(checkRepeatToolDTO, reCheckSearchResultPojo);
                    checkRepeatToolPojoList.add(checkRepeatToolPojo);
                    if (enableFormList.size() == 1) {
                        //需要给只有一个模板启用标识
                        checkRepeatToolVO.setSingletonTemplate(1);
                    }
                    checkRepeatToolVO.setMenuName(paasMenuEntity.getName());
                    checkRepeatToolVO.setBusinessType(businessType);
                    enableFormList.forEach(item -> {
                        if (aggDataForm.contains(item.getId()) && !Objects.equals(item.getId(), firstFormId)) {
                            CheckRepeatToolPojo checkRepeatTool = new CheckRepeatToolPojo();
                            checkRepeatTool.setFormId(item.getId());
                            checkRepeatTool.setFormName(item.getName());
                            checkRepeatTool.setSaasMark(item.getSaasMark());
                            checkRepeatToolPojoList.add(checkRepeatTool);
                        }
                    });
                    checkRepeatToolVO.setCheckList(checkRepeatToolPojoList);
                }
            }
        } catch (Exception e) {
            LOG.error("toolCheckRepeat常用查重搜索出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());

        }
        return checkRepeatToolVO;
    }

    /**
     * 搜索查重(常用) 通过切换具体表单，传入表单ID
     *
     * @param checkRepeatToolDTO
     * @author lijiangren
     * @date 2022-11-14 20 55
     */
    @Override
     public CheckRepeatToolPojo searchCommonRecheckByFormId(CheckRepeatToolDTO checkRepeatToolDTO) throws XbbException {
        CheckRepeatToolPojo checkRepeatToolPojo = new CheckRepeatToolPojo();
        try {
            String corpid = checkRepeatToolDTO.getCorpid();
            Integer businessType = checkRepeatToolDTO.getBusinessType();
            Long formId = checkRepeatToolDTO.getDataFormId();
            if (Objects.isNull(checkRepeatToolDTO.getCheckValue())) {
                throw new XbbException(RecheckingErrorCodeEnum.API_ERROR_330001);
            }
            // 公司是否开启了工作流
            boolean isOpenWorkflow = commonHelp.isOpenWorkFlow(corpid);
            checkRepeatToolDTO.setIsOpenWorkflow(isOpenWorkflow);
            Integer feeType = packageHelp.getFeeType(corpid);
            checkRepeatToolDTO.setFeeType(feeType);
            RecheckSearchStrategy recheckSearchStrategy = recheckSearchFactory.getRecheckSearchStrategy(businessType);

            List<Long> aggDataForm = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            aggDataForm.add(formId);
            //返回高亮字段
            List<RepeatSearchFieldPojo> repeatSearchFieldPojos = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            //返回字段定死电话和名称
            repeatSearchFieldPojos.add(new RepeatSearchFieldPojo(formId, StringConstant.NAME, checkRepeatToolDTO.getCheckValue()));
            repeatSearchFieldPojos.add(new RepeatSearchFieldPojo(formId, StringConstant.PHONE, checkRepeatToolDTO.getCheckValue()));

            XbbAggregatedPage<PaasFormDataEntityExt> esEntities = getCommonDuplicateData(aggDataForm, checkRepeatToolDTO);
            List<PaasProcessDataEntity> paasProcessDataEntityList = new ArrayList<>();
            if (recheckingRuleHelp.checkSearchApproval(checkRepeatToolDTO)) {
                // 没有开启工作流的时候，需要在流程数据表查询审批中的数据
                RepeatApprovalPojo repeatApprovalPojo = new RepeatApprovalPojo(checkRepeatToolDTO.getCheckValue(), formId, businessType, corpid);
                paasProcessDataEntityList = getInApprovalData(null,RecheckMatchWayEnum.FUZZY_MATCH.getCode(), BasicConstant.ZERO,aggDataForm,repeatApprovalPojo);
            }
            if (CollectionsUtil.isNotEmpty(esEntities.getContent()) || CollectionsUtil.isNotEmpty(paasProcessDataEntityList) ) {
                ReCheckSearchResultPojo reCheckSearchResultPojo = new ReCheckSearchResultPojo();
                reCheckSearchResultPojo.setFirstFormId(formId);
                reCheckSearchResultPojo.setPaasFormDataEntityList(esEntities.getContent());
                reCheckSearchResultPojo.setPartPaasProcessData(paasProcessDataEntityList);
                reCheckSearchResultPojo.setRepeatSearchFieldList(repeatSearchFieldPojos);
                PaasFormEntityExt paasFormEntity = paasFormModel.getByKey(formId, corpid);
                reCheckSearchResultPojo.setPaasFormEntityExt(paasFormEntity);

                //查重结果集策略
                checkRepeatToolPojo = recheckSearchStrategy.repeatResult(checkRepeatToolDTO, reCheckSearchResultPojo);

            }
            checkRepeatToolPojo.setBusinessType(businessType);

        } catch (Exception e) {
            LOG.error("通过表单ID,toolCheckRepeat常用查重搜索出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return checkRepeatToolPojo;
    }

    /**
     * 高级查重搜索
     *
     * @param seniorReCheckSearchDTO
     * @throws
     * @return
     * @author lijiangren
     * @date 2022-11-15 09 18
     * @since
     * @version
     */
    @Override
    public CheckRepeatToolVO searchSeniorRecheck(SeniorReCheckSearchDTO seniorReCheckSearchDTO) throws XbbException {
        CheckRepeatToolVO checkRepeatToolVO = null;
        try {
            checkRepeatToolVO = new CheckRepeatToolVO();
            String corpid = seniorReCheckSearchDTO.getCorpid();
            Integer businessType = seniorReCheckSearchDTO.getBusinessType();
            Long dataId = seniorReCheckSearchDTO.getDataId();
            Long taskId = seniorReCheckSearchDTO.getTaskId();
            // 公司是否开启了工作流
            boolean isOpenWorkflow = commonHelp.isOpenWorkFlow(corpid);
            seniorReCheckSearchDTO.setIsOpenWorkflow(isOpenWorkflow);
            Integer feeType = packageHelp.getFeeType(corpid);
            seniorReCheckSearchDTO.setFeeType(feeType);
            List<PaasFormEntityExt> enableFormList = paasFormModel.getFormListDistributor(corpid, businessType, BasicConstant.ZERO);
            List<SeniorCheckRepeatSearchPojo> searchFieldList = seniorReCheckSearchDTO.getSearchList();
            if (CollectionsUtil.isEmpty(searchFieldList)) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            RecheckSearchStrategy recheckSearchStrategy = recheckSearchFactory.getRecheckSearchStrategy(businessType);
            List<RepeatSearchFieldPojo> repeatSearchFieldList = recheckingRuleHelp.packageSearchFieldByBusinessType(seniorReCheckSearchDTO);
            List<Long> aggDataFormIds = getSeniorRepeatAggDataFormId(seniorReCheckSearchDTO, repeatSearchFieldList);
            // 没有开启工作流的时候，需要在流程数据表查询审批中的数据
            List<PaasProcessDataEntity> paasProcessDataEntityList = new ArrayList<>();
            CheckRepeatToolDTO checkRepeatToolDTO = new CheckRepeatToolDTO();
            BeanUtil.copyProperties(seniorReCheckSearchDTO,checkRepeatToolDTO);
            if (recheckingRuleHelp.checkSearchApproval(checkRepeatToolDTO)) {
                //还要判断查重字段是否有text_1字段
                SeniorCheckRepeatSearchPojo seniorCheckRepeatSearchPojo = searchFieldList.stream().filter(s -> Objects.equals(s.getAttr(), StringConstant.NAME)).findAny().orElse(null);
                if (Objects.nonNull(seniorCheckRepeatSearchPojo)) {
                    //判断该字段是否模板内还是全局判重
                    RecheckAttrRulePojo recheckAttrRulePojo = recheckingRuleHelp.checkNameRecheckType(seniorReCheckSearchDTO.getFormId(), businessType, corpid);
                    if (Objects.nonNull(recheckAttrRulePojo)) {
                        RepeatApprovalPojo repeatApprovalPojo = new RepeatApprovalPojo(seniorCheckRepeatSearchPojo.getValue(),businessType, corpid);
                        if (Objects.equals(BasicConstant.ZERO,recheckAttrRulePojo.getIsAll())) {
                            repeatApprovalPojo.setFormId(seniorReCheckSearchDTO.getFormId());
                        }
                        paasProcessDataEntityList = getInApprovalData(taskId,recheckAttrRulePojo.getMatchWay(),recheckAttrRulePojo.getIsAll(),aggDataFormIds,repeatApprovalPojo);
                    }
                }
            }
            //移除未启用表单模版查出来的脏数据
            List<Long> formIds = enableFormList.stream().map(PaasFormEntityExt::getId).collect(Collectors.toList());
            aggDataFormIds.retainAll(formIds);

            if (CollectionsUtil.isNotEmpty(aggDataFormIds) && CollectionsUtil.isNotEmpty(repeatSearchFieldList)) {
                Long firstFormId = aggDataFormIds.get(0);
                //没有权限
                PaasFormEntityExt paasFormEntityExt= enableFormList.stream().filter(item -> Objects.equals(item.getId(),firstFormId)).findAny().orElse(null);
                PaasMenuEntity paasMenuEntity = paasMenuModel.getByKey(paasFormEntityExt.getMenuId(), corpid);

                if (seniorReCheckSearchDTO.getIsClueLinkCustomer() || seniorReCheckSearchDTO.getIsClueLinkCustomer()) {
                    boolean permissionFlag = recheckingRuleHelp.checkMenuPermission(businessType, checkRepeatToolDTO.getLoginUser());
                    if (!permissionFlag) {
                        checkRepeatToolVO.setMenuName(paasMenuEntity.getName());
                        checkRepeatToolVO.setBusinessType(businessType);
                        checkRepeatToolVO.setNoPermission(1);
                        checkRepeatToolVO.setCheckList(new ArrayList<>());
                        return checkRepeatToolVO;
                    }
                }

                XbbAggregatedPage<PaasFormDataEntityExt> esEntities =  getSeniorDuplicateData(aggDataFormIds, repeatSearchFieldList, seniorReCheckSearchDTO);
                //判断是否有审批,查重当前表单的审批数据
                List<PaasProcessDataEntity> partPaasProcessData = new ArrayList<>();
                if (CollectionsUtil.isNotEmpty(paasProcessDataEntityList)) {
                    partPaasProcessData = paasProcessDataEntityList.stream().filter(item -> Objects.equals(firstFormId, item.getFormId())).collect(Collectors.toList());
                }
                if (CollectionsUtil.isNotEmpty(esEntities.getContent()) || CollectionsUtil.isNotEmpty(partPaasProcessData)) {
                    List<CheckRepeatToolPojo> checkRepeatToolPojoList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                    ReCheckSearchResultPojo reCheckSearchResultPojo = new ReCheckSearchResultPojo();
                    reCheckSearchResultPojo.setFirstFormId(firstFormId);
                    reCheckSearchResultPojo.setPaasFormDataEntityList(esEntities.getContent());
                    reCheckSearchResultPojo.setPartPaasProcessData(partPaasProcessData);
                    reCheckSearchResultPojo.setRepeatSearchFieldList(repeatSearchFieldList);
                    reCheckSearchResultPojo.setPaasFormEntityExt(paasFormEntityExt);
                    //查重结果集策略
                    CheckRepeatToolPojo checkRepeatToolPojo = recheckSearchStrategy.repeatResult(checkRepeatToolDTO, reCheckSearchResultPojo);
                    checkRepeatToolPojoList.add(checkRepeatToolPojo);
                    if (enableFormList.size() == 1) {
                        //需要给只有一个模板启用标识
                        checkRepeatToolVO.setSingletonTemplate(1);
                    }
                    checkRepeatToolVO.setMenuName(paasMenuEntity.getName());
                    checkRepeatToolVO.setBusinessType(businessType);
                    enableFormList.forEach(item -> {
                        if (aggDataFormIds.contains(item.getId()) && !Objects.equals(item.getId(), firstFormId)) {
                            CheckRepeatToolPojo checkRepeatTool = new CheckRepeatToolPojo();
                            checkRepeatTool.setFormId(item.getId());
                            checkRepeatTool.setFormName(item.getName());
                            checkRepeatTool.setSaasMark(item.getSaasMark());
                            checkRepeatToolPojoList.add(checkRepeatTool);
                        }
                    });
                    //编辑界面时，将当前的编辑数据移除
                    if (Objects.nonNull(dataId)){
                        //dataId不为空时代表是编辑界面
                        CheckRepeatToolPojo checkRepeatToolPojoEdit = checkRepeatToolPojoList.get(0);
                        List<Map> dataList = checkRepeatToolPojoEdit.getDataList();
                        if (CollectionsUtil.isNotEmpty(dataList)){
                            dataList.removeIf(item -> Objects.equals(dataId, item.get("id")));
                        }
                    }
                    checkRepeatToolVO.setCheckList(checkRepeatToolPojoList);
                } else {
                    return checkRepeatToolVO;
                }
            }

        } catch (Exception e) {
            LOG.error("toolSeniorCheckRepeat高级查重搜索出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return checkRepeatToolVO;
    }

    /**
     * 搜索查重(高级)切换tab页通过具体的formId
     *
     * @param seniorReCheckSearchDTO
     * @author lijiangren
     * @date 2022-11-14 20 55
     */
    @Override
    public CheckRepeatToolPojo searchSeniorRecheckByFormId(SeniorReCheckSearchDTO seniorReCheckSearchDTO) throws XbbException {
        CheckRepeatToolPojo checkRepeatToolPojo = new CheckRepeatToolPojo();
        try {
            String corpid = seniorReCheckSearchDTO.getCorpid();
            Integer businessType = seniorReCheckSearchDTO.getBusinessType();
            Long dataFormId = seniorReCheckSearchDTO.getDataFormId();
            // 公司是否开启了工作流
            boolean isOpenWorkflow = commonHelp.isOpenWorkFlow(corpid);
            seniorReCheckSearchDTO.setIsOpenWorkflow(isOpenWorkflow);
            Integer feeType = packageHelp.getFeeType(corpid);
            seniorReCheckSearchDTO.setFeeType(feeType);
            List<SeniorCheckRepeatSearchPojo> searchFieldList = seniorReCheckSearchDTO.getSearchList();
            if (CollectionsUtil.isEmpty(searchFieldList)) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            RecheckSearchStrategy recheckSearchStrategy = recheckSearchFactory.getRecheckSearchStrategy(businessType);
            List<RepeatSearchFieldPojo> repeatSearchFieldList = recheckingRuleHelp.packageSearchFieldByBusinessType(seniorReCheckSearchDTO);
            List<Long> aggDataFormIds = new ArrayList<>();
            aggDataFormIds.add(dataFormId);
            if (CollectionsUtil.isNotEmpty(aggDataFormIds) && CollectionsUtil.isNotEmpty(repeatSearchFieldList)) {
                XbbAggregatedPage<PaasFormDataEntityExt> esEntities = getSeniorDuplicateData(aggDataFormIds, repeatSearchFieldList, seniorReCheckSearchDTO);

                List<PaasProcessDataEntity> paasProcessDataEntityList = new ArrayList<>();
                if (recheckingRuleHelp.checkSearchApproval(seniorReCheckSearchDTO)) {
                    //还要判断查重字段是否有text_1字段
                    SeniorCheckRepeatSearchPojo seniorCheckRepeatSearchPojo = searchFieldList.stream().filter(s -> Objects.equals(s.getAttr(), "text_1")).findAny().orElse(null);
                    //判断该字段是否模板内还是全局判重
                    RecheckAttrRulePojo recheckAttrRulePojo = recheckingRuleHelp.checkNameRecheckType(seniorReCheckSearchDTO.getFormId(), businessType, corpid);
                    if (Objects.nonNull(recheckAttrRulePojo) && Objects.nonNull(seniorCheckRepeatSearchPojo)) {
                        RepeatApprovalPojo repeatApprovalPojo = new RepeatApprovalPojo(seniorCheckRepeatSearchPojo.getValue(),businessType, corpid);
                        //只查切换模糊的审批数据
                        repeatApprovalPojo.setFormId(seniorReCheckSearchDTO.getDataFormId());
                        paasProcessDataEntityList = getInApprovalData(null,recheckAttrRulePojo.getMatchWay(),BasicConstant.ZERO,aggDataFormIds,repeatApprovalPojo);
                    }
                }
                if (CollectionsUtil.isNotEmpty(esEntities.getContent()) || CollectionsUtil.isNotEmpty(paasProcessDataEntityList)) {
                    ReCheckSearchResultPojo reCheckSearchResultPojo = new ReCheckSearchResultPojo();
                    reCheckSearchResultPojo.setFirstFormId(dataFormId);
                    reCheckSearchResultPojo.setPaasFormDataEntityList(esEntities.getContent());
                    reCheckSearchResultPojo.setPartPaasProcessData(paasProcessDataEntityList);
                    reCheckSearchResultPojo.setRepeatSearchFieldList(repeatSearchFieldList);
                    PaasFormEntityExt paasFormEntity = paasFormModel.getByKey(dataFormId, corpid);
                    reCheckSearchResultPojo.setPaasFormEntityExt(paasFormEntity);
                    //查重结果集策略
                    checkRepeatToolPojo = recheckSearchStrategy.repeatResult(seniorReCheckSearchDTO, reCheckSearchResultPojo);
                    checkRepeatToolPojo.setBusinessType(businessType);
                    return checkRepeatToolPojo;
                }
            }
        } catch (Exception e) {
            LOG.error("toolSeniorCheckRepeatByFormId高级查重搜索出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return checkRepeatToolPojo;
    }

    /**
     * 查重工具获取查重参数
     *
     * @param repeatToolFieldGetDTO
     * @return
     * @throws XbbException
     */
    @Override
    public RepeatFieldListVO getRepeatField(RepeatToolFieldGetDTO repeatToolFieldGetDTO) throws XbbException {
        RepeatFieldListVO repeatFieldListVO = null;
        try {
            repeatFieldListVO = new RepeatFieldListVO();
            Integer businessType = repeatToolFieldGetDTO.getBusinessType();
            Long formId = repeatToolFieldGetDTO.getFormId();
            String corpid = repeatToolFieldGetDTO.getCorpid();

            List<RepeatFieldPojo> repeatFieldPojoList = new ArrayList<>();

            if (Objects.equals(Integer.valueOf(formId.toString()), BasicConstant.ZERO)) {
                //常用查重，常用查询不知道具体表单模板，这里直接用formId=0作为标识
                //判断是否开启常用查重,如果没数据库没查到，那么默认就是开启,机会默认关闭
                Integer normalEnable = recheckingRuleModel.getNormalEnable(corpid, businessType);
                if (Objects.equals(BasicConstant.ONE, normalEnable)) {
                    if (Objects.equals(businessType,XbbRefTypeEnum.SALES_OPPORTUNITY.getCode())) {
                        RepeatFieldPojo nameRepeatFieldPojo = new RepeatFieldPojo(StringConstant.NAME,SalesOpportunityEnum.NAME.getAttrName(),FieldTypeEnum.TEXT.getType(),"");
                        repeatFieldPojoList.add(nameRepeatFieldPojo);
                    } else {
                        String Attr = StringConstant.NAME;
                        String AttrName = CustomerManagementEnum.NAME.getAttrName();
                        String phoneAttr = StringConstant.PHONE;
                        String phoneAttrName = CustomerManagementEnum.PHONE.getAttrName()+ "("+ClueEnum.PHONE.getAttrName()+")";
                        if (Objects.equals(businessType,XbbRefTypeEnum.CLUE.getCode())) {
                            AttrName = ClueEnum.COMPANY_NAME.getAttrName();
                            phoneAttrName = ClueEnum.PHONE.getAttrName()+ "("+ClueEnum.PHONE.getAttrName()+")";
                        } else if (Objects.equals(businessType,XbbRefTypeEnum.CONTACT.getCode())) {
                            AttrName = ContactEnum.NAME.getAttrName();
                            phoneAttrName = ContactEnum.PHONE.getAttrName()+ "("+ClueEnum.PHONE.getAttrName()+")";
                        }
                        RepeatFieldPojo nameRepeatFieldPojo = new RepeatFieldPojo(Attr,AttrName,FieldTypeEnum.TEXT.getType(),"");
                        RepeatFieldPojo phoneRepeatFieldPojo = new RepeatFieldPojo(phoneAttr,phoneAttrName,FieldTypeEnum.TEXT.getType(),"");
                        repeatFieldPojoList.add(nameRepeatFieldPojo);
                        repeatFieldPojoList.add(phoneRepeatFieldPojo);
                    }
                }

            } else {
                //高级查重获取查重字段
                RecheckingRuleEntity recheckingRule = recheckingRuleModel.getRuleListByFormId(corpid, formId, businessType);
                if (Objects.isNull(recheckingRule)) {
                    return repeatFieldListVO;
                }
                if (Objects.nonNull(recheckingRule.getCheckToolRule())) {
                    PaasFormExplainEntity explainEntity = paasFormExplainModel.getByFormId(formId, corpid);
                    List<FieldAttrEntity> explainList = JSONArray.parseArray(explainEntity.getExplains(), FieldAttrEntity.class);
                    explainList.removeIf(item -> (Objects.equals(item.getEditHide(), BasicConstant.ONE)
                            || !Objects.equals(item.getIsOpen(), BasicConstant.ONE)
                    ));
                    Map<String, FieldAttrEntity> explainMap = ExplainUtil.getAllSubFormExplainMap(JSON.toJSONString(explainList));

                    String checkToolRule = recheckingRule.getCheckToolRule();
                    List<RecheckFormRulePojo> recheckFormRuleList = JSONArray.parseArray(checkToolRule, RecheckFormRulePojo.class);
                    for (RecheckFormRulePojo recheckFormRulePojo : recheckFormRuleList) {
                        RecheckAttrRulePojo recheckAttrRule = recheckingRuleHelp.getRecheckAttrRuleByBusinessType(businessType, recheckFormRulePojo);
                        if (Objects.nonNull(recheckAttrRule)) {
                            //查重字段
                            String attr = recheckAttrRule.getAttr();
                            //判断改字段是否启用
                            FieldAttrEntity fieldAttrEntity = explainMap.get(attr);
                            if (Objects.nonNull(fieldAttrEntity)) {
                                RepeatFieldPojo repeatFieldPojo = new RepeatFieldPojo();
                                repeatFieldPojo.setAttr(attr);
                                repeatFieldPojo.setFieldType(fieldAttrEntity.getFieldType());

                                if (attr.contains(StringConstant.POINT)) {
                                    String[] split = attr.split("\\.");
                                    if (split != null && split.length > 0) {
                                        FieldAttrEntity parentFieldAttrEntity = explainMap.get(split[0]);
                                        repeatFieldPojo.setAttrName(parentFieldAttrEntity.getAttrName() + "(" + fieldAttrEntity.getAttrName() + ")");
                                    }
                                } else {
                                    repeatFieldPojo.setAttrName(fieldAttrEntity.getAttrName());
                                }

                                repeatFieldPojo.setValue("");
                                repeatFieldPojoList.add(repeatFieldPojo);
                            }

                        }
                    }

                }
            }
            repeatFieldListVO.setRepeatFieldList(repeatFieldPojoList);
        } catch (Exception e) {
            LOG.error("getRepeatField查询查重字段数据出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return repeatFieldListVO;
    }

    @Override
    public CheckRepeatToolVO mobileToolCheckRepeat(MobileCheckRepeatToolDTO checkRepeatToolDTO) throws XbbException {
        CheckRepeatToolVO checkRepeatToolVO = new CheckRepeatToolVO();
        checkRepeatToolDTO = receiveParam(checkRepeatToolDTO);
        String value = null;
        //判断是高级查重还是普通查重
        String checkValue = checkRepeatToolDTO.getCheckValue();
        List<SeniorCheckRepeatSearchPojo> searchList = checkRepeatToolDTO.getSearchList();
        //两个都没有数据不进行查询
        if (StringUtil.isEmpty(checkValue) && CollectionsUtil.isEmpty(searchList)) {
            return checkRepeatToolVO;
        }
        if (StringUtil.isEmpty(checkValue) && CollectionsUtil.isNotEmpty(searchList)) {
            for (SeniorCheckRepeatSearchPojo seniorCheckRepeatSearchPojo : searchList) {
                if (Objects.equals("subForm_1.text_2", seniorCheckRepeatSearchPojo.getAttr())) {
                    value = seniorCheckRepeatSearchPojo.getValue();
                }
            }
            //高级查重
            SeniorReCheckSearchDTO seniorReCheckSearchDTO  = new SeniorReCheckSearchDTO();
            BeanUtil.copyProperties(checkRepeatToolDTO, seniorReCheckSearchDTO);
            checkRepeatToolVO = searchSeniorRecheck(seniorReCheckSearchDTO);
        } else if (StringUtil.isNotEmpty(checkValue) && CollectionsUtil.isEmpty(searchList)) {
            value = checkValue;
            //普通查重
            CheckRepeatToolDTO newCheckRepeatToolDTO = new CheckRepeatToolDTO();
            BeanUtil.copyProperties(checkRepeatToolDTO, newCheckRepeatToolDTO);
            checkRepeatToolVO = searchCommonRecheck(newCheckRepeatToolDTO);
        }
        List<CheckRepeatToolPojo> checkList = checkRepeatToolVO.getCheckList();
        Integer businessType = checkRepeatToolVO.getBusinessType();
        if (CollectionsUtil.isNotEmpty(checkList)) {
            CheckRepeatToolPojo checkRepeatToolPojo = checkList.get(0);
            List<Map> dataList = checkRepeatToolPojo.getDataList();
            List<CheckRepeatToolHeadPojo> headList = checkRepeatToolPojo.getHeadList();
            //返回参数处理
            checkRepeatToolPojo = getMobileParamDeal(dataList, headList, checkRepeatToolPojo, value, businessType);
            checkRepeatToolPojo.setHeadList(null);
        }
        checkRepeatToolVO.setCheckList(checkList);
        return checkRepeatToolVO;
    }

    @Override
    public CheckRepeatToolPojo mobileToolCheckRepeatByFormId(MobileCheckRepeatToolDTO checkRepeatToolDTO) throws XbbException {
        CheckRepeatToolPojo checkRepeatToolPojo = new CheckRepeatToolPojo();
        checkRepeatToolDTO = receiveParam(checkRepeatToolDTO);
        String value = null;
        //判断是高级查重还是普通查重
        String checkValue = checkRepeatToolDTO.getCheckValue();
        List<SeniorCheckRepeatSearchPojo> searchList = checkRepeatToolDTO.getSearchList();
        //两个都没有数据不进行查询
        if (StringUtil.isEmpty(checkValue) && CollectionsUtil.isEmpty(searchList)) {
            return checkRepeatToolPojo;
        }
        if (StringUtil.isEmpty(checkValue) && CollectionsUtil.isNotEmpty(searchList)) {
            for (SeniorCheckRepeatSearchPojo seniorCheckRepeatSearchPojo : searchList) {
                if (Objects.equals("subForm_1.text_2", seniorCheckRepeatSearchPojo.getAttr())) {
                    value = seniorCheckRepeatSearchPojo.getValue();
                }
            }
            //高级查重
            SeniorReCheckSearchDTO seniorReCheckSearchDTO  = new SeniorReCheckSearchDTO();
            BeanUtil.copyProperties(checkRepeatToolDTO, seniorReCheckSearchDTO);
            checkRepeatToolPojo = searchSeniorRecheckByFormId(seniorReCheckSearchDTO);
        } else if (StringUtil.isNotEmpty(checkValue) && CollectionsUtil.isEmpty(searchList)) {
            value = checkValue;
            //普通查重
            CheckRepeatToolDTO newCheckRepeatToolDTO = new CheckRepeatToolDTO();
            BeanUtil.copyProperties(checkRepeatToolDTO, newCheckRepeatToolDTO);
            checkRepeatToolPojo = searchCommonRecheckByFormId(newCheckRepeatToolDTO);
        }
        if (Objects.nonNull(checkRepeatToolPojo)) {
            Integer businessType = checkRepeatToolPojo.getBusinessType();
            List<Map> dataList = checkRepeatToolPojo.getDataList();
            List<CheckRepeatToolHeadPojo> headList = checkRepeatToolPojo.getHeadList();
            //返回参数处理
            checkRepeatToolPojo = getMobileParamDeal(dataList, headList, checkRepeatToolPojo, value, businessType);
            checkRepeatToolPojo.setHeadList(null);
            checkRepeatToolPojo.setDataList(dataList);
        }
        return checkRepeatToolPojo;
    }


    public CheckRepeatToolPojo getMobileParamDeal(List<Map> dataList, List<CheckRepeatToolHeadPojo> headList, CheckRepeatToolPojo checkRepeatToolPojo, String value, Integer businessType) {
        JSONObject superScript = new JSONObject();
        for (Map map : dataList) {
            JSONObject others = new JSONObject();
            List<SummaryDataPoJo> summaryList = new ArrayList<>();
            List<FormTitlePoJo> titleList = new ArrayList<>();
            int count = 1;
            Object idValue = map.get("id");
            Object del = map.get("del");
            for (CheckRepeatToolHeadPojo checkRepeatToolHeadPojo : headList) {
                String attr = checkRepeatToolHeadPojo.getAttr();
                Integer fieldType = checkRepeatToolHeadPojo.getFieldType();
                //操作字段单独处理
                if (Objects.equals(attr, "option")) {
                    Set set = map.keySet();
                    if (set.contains("isGrab")) {
                        others.put("isGrab", true);
                    } else if (set.contains("isAddTeam")) {
                        others.put("isAddTeam", true);
                    }
                } else {
                    if (Objects.equals(BasicConstant.ONE, count)) {
                        FormTitlePoJo formTitlePoJo = new FormTitlePoJo();
                        formTitlePoJo.setAttrName(checkRepeatToolHeadPojo.getAttrName());
                        formTitlePoJo.setAttr(attr);
                        formTitlePoJo.setFieldType(fieldType);
                        Object o = map.get(attr);
                        List<Object> values = new ArrayList<>();
                        values.add(o);
                        values = mobileSpecialDeal(attr, fieldType, values, o, value, businessType);
                        formTitlePoJo.setValue(values);
                        titleList.add(formTitlePoJo);
                        count++;
                    } else {
                        SummaryDataPoJo summaryDataPoJo = new SummaryDataPoJo();
                        summaryDataPoJo.setAttrName(checkRepeatToolHeadPojo.getAttrName());
                        summaryDataPoJo.setFieldType(fieldType);
                        summaryDataPoJo.setAttr(attr);
                        Object o = map.get(attr);
                        List<Object> values = new ArrayList<>();
                        values.add(o);
                        //需特殊处理字段
                        values = mobileSpecialDeal(attr, fieldType, values, o, value, businessType);
                        summaryDataPoJo.setValue(values);
                        summaryList.add(summaryDataPoJo);
                    }
                }
            }
            map.clear();
            map.put("others", others);
            map.put("summaryList", summaryList);
            map.put("titleList", titleList);
            map.put("superScript", superScript);
            map.put("del", del);
            map.put("id", idValue);
        }
        return checkRepeatToolPojo;
    }

    public List<Object> mobileSpecialDeal(String attr, Integer fieldType, List<Object> values, Object o, String value, Integer businessType) {
        //日期、创建时间、更新时间字段改为web端直接处理
        JSONArray jsonArray = new JSONArray();
        //关联数据类型或者关联业务单选类型字段
        if (Objects.equals(fieldType,FieldTypeEnum.LINK_DATA.getType()) || Objects.equals(fieldType,FieldTypeEnum.LINK_BUSINESS_SINGLE.getType())) {
            if (Objects.nonNull(o)) {
                jsonArray = JSONArray.parseArray(o.toString());
                if (Objects.equals(BasicConstant.ZERO, jsonArray.size())) {
                    return values;
                }
                List<ContactLinkCustomerPojo> contactLinkCustomerPojos = jsonArray.toJavaList(ContactLinkCustomerPojo.class);
                values.remove(o);
                values.add(contactLinkCustomerPojos.get(0).getName());
            }
        } else if (Objects.equals(attr, "subForm_1")) {
            String phoneValue = null;
            if (Objects.nonNull(o)) {
                jsonArray = JSONArray.parseArray(o.toString());
                if (Objects.equals(BasicConstant.ZERO, jsonArray.size())) {
                    values.remove(o);
                    return values;
                }
                List<PhoneSubformPojo> phoneSubformPojos = jsonArray.toJavaList(PhoneSubformPojo.class);
                for (PhoneSubformPojo phoneSubformPojo : phoneSubformPojos) {
                    String text_2 = phoneSubformPojo.getText_2();
                    values.remove(o);
                    //判断电话值和查重电话值是否一致
                    if (Objects.equals(text_2, value)) {
//                        values.add(text_2);
                        phoneValue = text_2;
                    }
                }
                if (Objects.isNull(phoneValue)) {
                    //没有与传入的电话值完全相同的数据
                    String text_2 = phoneSubformPojos.get(0).getText_2();
                    if (phoneSubformPojos.size() > 1) {
                        values.add(text_2 + "...");
                    } else if (phoneSubformPojos.size() == 1) {
                        values.add(text_2);
                    }
                } else {
                    if (phoneSubformPojos.size() > 1) {
                        values.add(phoneValue + "...");
                    } else if (phoneSubformPojos.size() == 1) {
                        values.add(phoneValue);
                    }
                }
            }
        }else if (Objects.equals(FieldTypeEnum.RADIO_GROUP.getType(), fieldType) || Objects.equals(FieldTypeEnum.COMBO.getType(), fieldType) || Objects.equals(FieldTypeEnum.STAGE_THRUSTER.getType(), fieldType)){
            if (Objects.nonNull(o) && o instanceof ItemPoJo){
                values.remove(o);
                values.add(((ItemPoJo)o).getText());
            }
        }else if (Objects.equals(FieldTypeEnum.COMBO_CHECK.getType(), fieldType) || Objects.equals(FieldTypeEnum.CHECKBOX_GROUP.getType(), fieldType)){
            if (Objects.nonNull(o) && o instanceof ItemPoJo){
                values.remove(o);
                values.add(((List<ItemPoJo>) o).stream().map(ItemPoJo::getText).collect(Collectors.toList()));
            }
        }
        return values;
    }


    public MobileCheckRepeatToolDTO receiveParam(MobileCheckRepeatToolDTO mobileCheckRepeatToolDTO) {
        Integer linkBusinessType = mobileCheckRepeatToolDTO.getLinkBusinessType();
        Integer businessType = mobileCheckRepeatToolDTO.getBusinessType();
        if (!Objects.equals(linkBusinessType, businessType)) {
            if (Objects.equals(XbbRefTypeEnum.CLUE.getCode(), linkBusinessType)) {
                mobileCheckRepeatToolDTO.setBusinessType(XbbRefTypeEnum.CLUE.getCode());
                mobileCheckRepeatToolDTO.setIsClueLinkContact(false);
                mobileCheckRepeatToolDTO.setIsClueLinkCustomer(false);
            } else if (Objects.equals(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), linkBusinessType)) {
                mobileCheckRepeatToolDTO.setBusinessType(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode());
                mobileCheckRepeatToolDTO.setIsClueLinkContact(false);
                mobileCheckRepeatToolDTO.setIsClueLinkCustomer(true);
            } else if (Objects.equals(XbbRefTypeEnum.CONTACT.getCode(), linkBusinessType)) {
                mobileCheckRepeatToolDTO.setBusinessType(XbbRefTypeEnum.CONTACT.getCode());
                mobileCheckRepeatToolDTO.setIsClueLinkContact(true);
                mobileCheckRepeatToolDTO.setIsClueLinkCustomer(false);
            }
        } else {
            mobileCheckRepeatToolDTO.setIsClueLinkContact(false);
            mobileCheckRepeatToolDTO.setIsClueLinkCustomer(false);
        }
        return mobileCheckRepeatToolDTO;
    }

    /**
     * 封装查重走审批的逻辑
     * @author lijiangren
     * @date 2022-11-16 09 32
     * @since
     * @version
     */
    private List<PaasProcessDataEntity> getInApprovalData(Long taskId, Integer matchWay, Integer isAll, List<Long> aggDataForm, RepeatApprovalPojo repeatApprovalPojo) {
        List<PaasProcessDataEntity> inApprovalData = new ArrayList<>();
        String corpid = repeatApprovalPojo.getCorpid();
        String checkValue = repeatApprovalPojo.getCheckValue();
        String attr = CustomerManagementEnum.NAME.getAttr();
        Integer businessType = repeatApprovalPojo.getBusinessType();
        Long formId = repeatApprovalPojo.getFormId();
        if (Objects.equals(BasicConstant.ZERO,isAll)) {
            //单个模版
            if (Objects.nonNull(matchWay) && Objects.equals(matchWay,RecheckMatchWayEnum.EXACT_MATCH.getCode())){
                //单模板精确
                inApprovalData = paasProcessDataModel.getInApprovalDataByFormId(businessType, attr, checkValue, corpid, formId);
            } else {
                inApprovalData = paasProcessDataModel.getInApprovalDataByLikeCondition(businessType, CustomerManagementEnum.NAME.getAttr(), checkValue, corpid, formId);
            }
        } else {
            //全部模版
            if (Objects.nonNull(matchWay) && Objects.equals(matchWay,RecheckMatchWayEnum.EXACT_MATCH.getCode())){
                //全模板精确
                inApprovalData = paasProcessDataModel.getInApprovalData(businessType, attr, checkValue, corpid);
            } else {
                inApprovalData = paasProcessDataModel.getInApprovalDataByLikeCondition(businessType, CustomerManagementEnum.NAME.getAttr(), checkValue, corpid, null);
            }
        }
        //将审批的当前数据移除,taskId不为空是审批点击同意处触发的请求
        if (Objects.nonNull(taskId)){
            if (CollectionsUtil.isNotEmpty(inApprovalData)){
                inApprovalData.removeIf(item -> Objects.equals(taskId, item.getTaskId()));
            }
        }
        //遍历审批数据、获取需要查询的模板
        if (CollectionsUtil.isNotEmpty(inApprovalData)) {
            inApprovalData.forEach(item -> {
                if (!aggDataForm.contains(item.getFormId())) {
                    aggDataForm.add(item.getFormId());
                }
            });
        }
        return inApprovalData;
    }

    /**
     * 常用查重获取可能存在搜索结果的表单
     * @author lijiangren
     * @date 2022-11-16 09 32
     * @since
     * @version
     */
    private List<Long> getCommonRepeatAggDataFormId(CheckRepeatToolDTO checkRepeatToolDTO, List<PaasFormEntityExt> enableFormList) throws XbbException {
        List<Long> aggDataForm = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        String corpid = checkRepeatToolDTO.getCorpid();
        Integer businessType = checkRepeatToolDTO.getBusinessType();
        Long dataFormId = checkRepeatToolDTO.getDataFormId();
        IndexTypeEnum indexTypeEnum = indexTypeModel.getIndexType(corpid, businessType, SaasMarkEnum.SAAS.getCode());

        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
        if (Objects.equals(businessType, XbbRefTypeEnum.CONTACT.getCode()) || Objects.equals(businessType, XbbRefTypeEnum.SALES_OPPORTUNITY.getCode())) {
            boolQueryBuilder.filter(termsQuery("del", Arrays.asList(DelEnum.NORMAL.getDel(), DelEnum.APPROVE_STATUS.getDel())));
        } else {
            boolQueryBuilder.filter(termsQuery("del", Arrays.asList(DelEnum.NORMAL.getDel(), DelEnum.DELETE.getDel(), DelEnum.APPROVE_STATUS.getDel())));
        }
        if (checkRepeatToolDTO.getIsOpenWorkflow()) {
            // 如果开启了工作流，审批中数据会进入主表，查重的时候可以直接从主表查到
            boolQueryBuilder.filter(termsQuery(FieldTypeEnum.FLOW_STATUS.getAlias(), Arrays.asList(FlowStatusEnum.NOT_NEED_APPROVAL.getType(), FlowStatusEnum.PASS.getType(), FlowStatusEnum.APPROVAL_PENDING.getType(), FlowStatusEnum.IN_APPROVAL.getType())));
        } else {
            boolQueryBuilder.filter(termsQuery(FieldTypeEnum.FLOW_STATUS.getAlias(), Arrays.asList(FlowStatusEnum.NOT_NEED_APPROVAL.getType(), FlowStatusEnum.PASS.getType())));
        }

        if (Objects.nonNull(dataFormId)) {
            //查重结果切换tab表单查询该表单下的数据
            boolQueryBuilder.filter(termQuery("formId", dataFormId));
        } else {
            if (CollectionsUtil.isNotEmpty(enableFormList)) {
                List<Long> formIds = enableFormList.stream().map(PaasFormEntityExt::getId).collect(Collectors.toList());
                boolQueryBuilder.filter(termsQuery("formId", formIds));
            } else {
                return  aggDataForm;
            }
        }

        boolQueryBuilder.should(termQuery("data." + CustomerManagementEnum.NAME.getAttr() + ".keyword", checkRepeatToolDTO.getCheckValue()).boost(2));
        if (!Objects.equals(businessType,XbbRefTypeEnum.SALES_OPPORTUNITY.getCode())) {
            boolQueryBuilder.should(termQuery("data." + CustomerManagementEnum.PHONE.getAttr() + ".text_2" + ".keyword", checkRepeatToolDTO.getCheckValue()).boost(2));
        }
        if (!(checkRepeatToolDTO.getIsClueLinkCustomer() || checkRepeatToolDTO.getIsClueLinkContact())) {
            boolQueryBuilder.should(matchPhraseQuery("data." + CustomerManagementEnum.NAME.getAttr(), checkRepeatToolDTO.getCheckValue()));
            if (!Objects.equals(businessType,XbbRefTypeEnum.SALES_OPPORTUNITY.getCode())) {
                boolQueryBuilder.should(matchPhraseQuery("data." + CustomerManagementEnum.PHONE.getAttr() + ".text_2", checkRepeatToolDTO.getCheckValue()));
            }
        }
        boolQueryBuilder.minimumShouldMatch(MINI_MUM_SHOULD_MATCH);
        boolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD, indexTypeEnum.getType()));
        sourceBuilder.size(0);
        sourceBuilder.query(boolQueryBuilder);
        sourceBuilder.trackTotalHits(false);
        TermsAggregationBuilder termsAggregation = AggregationBuilders.terms("aggs01").field("formId").size(10);

        sourceBuilder.aggregation(termsAggregation);
        SearchRequest searchRequest = new SearchRequest(indexTypeEnum.getIndex());
        searchRequest.source(sourceBuilder);
        JSONObject aggJson = xbbElasticsearchRestTemplate.getAggResponseJson(searchRequest);
        JSONArray resultArray = aggJson.getJSONObject("aggregations").getJSONObject("aggs01").getJSONArray("buckets");
        if (resultArray != null && !resultArray.isEmpty()) {
            for (Object object : resultArray) {
                if (object != null) {
                    JSONObject json = (JSONObject) object;
                    Long formId = json.getLong("key");
                    aggDataForm.add(formId);
                }
            }
        }
        return aggDataForm;
    }

    /**
     * 常用查重拼接固定搜索条件查重ES，返回结果集
     * @author lijiangren
     * @date 2022-11-16 09 32
     * @since
     * @version
     */
    private XbbAggregatedPage<PaasFormDataEntityExt> getCommonDuplicateData(List<Long> aggDataForm, CheckRepeatToolDTO checkRepeatToolDTO) throws XbbException {
        String corpid = checkRepeatToolDTO.getCorpid();
        Integer businessType = checkRepeatToolDTO.getBusinessType();
        Long firstFormId = aggDataForm.get(0);
        IndexTypeEnum indexTypeEnum = indexTypeModel.getIndexType(corpid, businessType, SaasMarkEnum.SAAS.getCode());

        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
        boolQueryBuilder.filter(termQuery("formId", firstFormId));
        if (Objects.equals(businessType, XbbRefTypeEnum.CONTACT.getCode()) || Objects.equals(businessType, XbbRefTypeEnum.SALES_OPPORTUNITY.getCode())) {
            boolQueryBuilder.filter(termsQuery("del", Arrays.asList(DelEnum.NORMAL.getDel(), DelEnum.APPROVE_STATUS.getDel())));
        } else {
            boolQueryBuilder.filter(termsQuery("del", Arrays.asList(DelEnum.NORMAL.getDel(), DelEnum.DELETE.getDel(), DelEnum.APPROVE_STATUS.getDel())));
        }
        if (checkRepeatToolDTO.getIsOpenWorkflow()) {
            // 如果开启了工作流，审批中数据会进入主表，查重的时候可以直接从主表查到
            boolQueryBuilder.filter(termsQuery(FieldTypeEnum.FLOW_STATUS.getAlias(), Arrays.asList(FlowStatusEnum.NOT_NEED_APPROVAL.getType(), FlowStatusEnum.PASS.getType(), FlowStatusEnum.APPROVAL_PENDING.getType(), FlowStatusEnum.IN_APPROVAL.getType())));
        } else {
            boolQueryBuilder.filter(termsQuery(FieldTypeEnum.FLOW_STATUS.getAlias(), Arrays.asList(FlowStatusEnum.NOT_NEED_APPROVAL.getType(), FlowStatusEnum.PASS.getType())));
        }

        boolQueryBuilder.should(termQuery("data." + CustomerManagementEnum.NAME.getAttr() + ".keyword", checkRepeatToolDTO.getCheckValue()).boost(2));
        if (!Objects.equals(businessType,XbbRefTypeEnum.SALES_OPPORTUNITY.getCode())) {
            boolQueryBuilder.should(termQuery("data." + CustomerManagementEnum.PHONE.getAttr() + ".text_2" + ".keyword", checkRepeatToolDTO.getCheckValue()).boost(2));
        }
        if (!(checkRepeatToolDTO.getIsClueLinkCustomer() || checkRepeatToolDTO.getIsClueLinkContact())) {
            boolQueryBuilder.should(matchPhraseQuery("data." + CustomerManagementEnum.NAME.getAttr(), checkRepeatToolDTO.getCheckValue()));
            if (!Objects.equals(businessType,XbbRefTypeEnum.SALES_OPPORTUNITY.getCode())) {
                boolQueryBuilder.should(matchPhraseQuery("data." + CustomerManagementEnum.PHONE.getAttr() + ".text_2", checkRepeatToolDTO.getCheckValue()));
            }
        }

        boolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD, indexTypeEnum.getType()));
        boolQueryBuilder.minimumShouldMatch(MINI_MUM_SHOULD_MATCH);
        Integer page = 1;
        Integer pageSize = 10;
        PageRequest pageRequest = EsUtil.setPage(sourceBuilder, (page - 1), pageSize);
        sourceBuilder.query(boolQueryBuilder);
        SearchRequest searchRequest = new SearchRequest(indexTypeEnum.getIndex());
        searchRequest.source(sourceBuilder);
        XbbAggregatedPage<PaasFormDataEntityExt> esEntities = xbbElasticsearchRestTemplate.queryForPages(pageRequest, searchRequest, PaasFormDataEntityExt.class);
        return esEntities;
    }

    private List<Long> getSeniorRepeatAggDataFormId(SeniorReCheckSearchDTO seniorReCheckSearchDTO, List<RepeatSearchFieldPojo> repeatSearchFieldPojos) throws XbbException {
        String corpid = seniorReCheckSearchDTO.getCorpid();
        Integer businessType = seniorReCheckSearchDTO.getBusinessType();
        Long dataId = seniorReCheckSearchDTO.getDataId();

        IndexTypeEnum indexTypeEnum = indexTypeModel.getIndexType(corpid,businessType, SaasMarkEnum.SAAS.getCode());
        IndexTypeEnum indexTypeSubEnum = indexTypeModel.getSubIndexType(corpid, businessType, SaasMarkEnum.SAAS.getCode());

        List<Long> aggDataForm = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        //拼接查重条件
        if (CollectionsUtil.isNotEmpty(repeatSearchFieldPojos)) {
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            BoolQueryBuilder boolQueryBuilder = boolQuery();
            boolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
            if (Objects.equals(businessType, XbbRefTypeEnum.CONTACT.getCode()) || Objects.equals(businessType, XbbRefTypeEnum.SALES_OPPORTUNITY.getCode())) {
                boolQueryBuilder.filter(termsQuery("del", Arrays.asList(DelEnum.NORMAL.getDel(), DelEnum.APPROVE_STATUS.getDel())));
            } else {
                boolQueryBuilder.filter(termsQuery("del", Arrays.asList(DelEnum.NORMAL.getDel(), DelEnum.DELETE.getDel(), DelEnum.APPROVE_STATUS.getDel())));
            }
            if (Objects.nonNull(dataId)) {
                boolQueryBuilder.mustNot(termQuery(FieldTypeEnum.DATAID.getAlias(), dataId));
            }
            if (seniorReCheckSearchDTO.getIsOpenWorkflow()) {
                // 如果开启了工作流，审批中数据会进入主表，查重的时候可以直接从主表查到
                boolQueryBuilder.filter(termsQuery(FieldTypeEnum.FLOW_STATUS.getAlias(), Arrays.asList(FlowStatusEnum.NOT_NEED_APPROVAL.getType(), FlowStatusEnum.PASS.getType(), FlowStatusEnum.APPROVAL_PENDING.getType(), FlowStatusEnum.IN_APPROVAL.getType())));
            } else {
                boolQueryBuilder.filter(termsQuery(FieldTypeEnum.FLOW_STATUS.getAlias(), Arrays.asList(FlowStatusEnum.NOT_NEED_APPROVAL.getType(), FlowStatusEnum.PASS.getType())));
            }
            for (RepeatSearchFieldPojo repeatSearchFieldPojo : repeatSearchFieldPojos) {
                BoolQueryBuilder queryBuilder = boolQuery();
                if (repeatSearchFieldPojo.getAttr().contains(StringConstant.POINT) && (Objects.equals(businessType,XbbRefTypeEnum.SALES_OPPORTUNITY.getCode()) || !Objects.equals(repeatSearchFieldPojo.getAttr(), StringConstant.PHONE))) {
                    HasChildQueryBuilder hasChildQueryBuilder = subFormQueryBuilder(indexTypeSubEnum.getType(), corpid, repeatSearchFieldPojo);
                    boolQueryBuilder.should(hasChildQueryBuilder);
                } else {
                    if (Objects.equals(RecheckMatchWayEnum.EXACT_MATCH.getCode(),repeatSearchFieldPojo.getMatchWay())) {
                        if (Objects.equals("serialNo",repeatSearchFieldPojo.getAttr())){
                            boolQueryBuilder.should(queryBuilder.must(termQuery("formId", repeatSearchFieldPojo.getFormId())).must(termQuery(repeatSearchFieldPojo.getAttr()+ StringConstant.POINT + StringConstant.KEY_WORD, repeatSearchFieldPojo.getValue())));
                        } else {
                            //精确查询
                            boolQueryBuilder.should(queryBuilder.must(termQuery("formId", repeatSearchFieldPojo.getFormId())).must(termQuery("data." + repeatSearchFieldPojo.getAttr() + StringConstant.POINT + StringConstant.KEY_WORD, repeatSearchFieldPojo.getValue())));
                        }
                    } else {
                        //模糊查询
                        BoolQueryBuilder likeQueryBuilder = boolQuery();
                        boolQueryBuilder.should(queryBuilder.must(termQuery("formId", repeatSearchFieldPojo.getFormId())).must(termQuery("data." + repeatSearchFieldPojo.getAttr() + StringConstant.POINT + StringConstant.KEY_WORD, repeatSearchFieldPojo.getValue())));
                        boolQueryBuilder.should(likeQueryBuilder.must(termQuery("formId", repeatSearchFieldPojo.getFormId())).must(matchPhraseQuery("data." + repeatSearchFieldPojo.getAttr(), repeatSearchFieldPojo.getValue())));
                    }
                }
            }
            boolQueryBuilder.minimumShouldMatch(MINI_MUM_SHOULD_MATCH);
            boolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD, indexTypeEnum.getType()));
            sourceBuilder.size(0);
            sourceBuilder.query(boolQueryBuilder);
            sourceBuilder.trackTotalHits(false);
            TermsAggregationBuilder termsAggregation = AggregationBuilders.terms("aggs01").field("formId").size(10);

            sourceBuilder.aggregation(termsAggregation);
            SearchRequest searchRequest = new SearchRequest(indexTypeEnum.getIndex());
            searchRequest.source(sourceBuilder);
            JSONObject aggJson = xbbElasticsearchRestTemplate.getAggResponseJson(searchRequest);

            JSONArray resultArray = aggJson.getJSONObject("aggregations").getJSONObject("aggs01").getJSONArray("buckets");
            if (resultArray != null && !resultArray.isEmpty()) {
                for (Object object : resultArray) {
                    if (object != null) {
                        JSONObject json = (JSONObject) object;
                        Long aggformId = json.getLong("key");
                        aggDataForm.add(aggformId);
                    }
                }
            }
        }
        return aggDataForm;
    }

    /**
     * 子表单的查询
     * @param indexType
     * @param corpid
     * @param repeatSearchFieldPojo
     * @return
     */
    public HasChildQueryBuilder subFormQueryBuilder(String indexType, String corpid, RepeatSearchFieldPojo repeatSearchFieldPojo) {
        String[] split = repeatSearchFieldPojo.getAttr().split("\\.");
        String childAttr = split[1];
        BoolQueryBuilder childQueryBuilder = boolQuery();
        childQueryBuilder.filter(termQuery("corpid.keyword", corpid));
        childQueryBuilder.filter(termQuery("del", 0));
        childQueryBuilder.filter(termQuery("formId", repeatSearchFieldPojo.getFormId()));
        if (Objects.equals(RecheckMatchWayEnum.EXACT_MATCH.getCode(), repeatSearchFieldPojo.getMatchWay())) {
            childQueryBuilder.filter(termQuery("data." + childAttr + ".keyword", repeatSearchFieldPojo.getValue()));
        } else {
            childQueryBuilder.filter(matchPhraseQuery("data." + childAttr, repeatSearchFieldPojo.getValue()));
        }


//        BoolQueryBuilder queryBuilder = boolQuery();
//        if (Objects.equals(RecheckMatchWayEnum.EXACT_MATCH.getCode(),repeatSearchFieldPojo.getMatchWay())) {
//            //精确查询
//            //childQueryBuilder.should(queryBuilder.must(termQuery("data." + repeatSearchFieldPojo.getAttr() + ".keyword", repeatSearchFieldPojo.getValue())));
//            childQueryBuilder.should(queryBuilder.must(termQuery("formId", repeatSearchFieldPojo.getFormId())).must(termQuery("data." + childAttr + ".keyword", repeatSearchFieldPojo.getValue())));
//
//            //teamQueryBuilder.filter(termQuery("data." + childAttr + ".keyword", repeatSearchFieldPojo.getValue()));
//        } else {
//            //模糊查询
//            BoolQueryBuilder likeQueryBuilder = boolQuery();
//            childQueryBuilder.should(queryBuilder.must(termQuery("formId", repeatSearchFieldPojo.getFormId())).must(termQuery("data." + childAttr + ".keyword", repeatSearchFieldPojo.getValue())));
//            childQueryBuilder.should(likeQueryBuilder.must(termQuery("formId", repeatSearchFieldPojo.getFormId())).must(matchPhraseQuery("data." + childAttr, repeatSearchFieldPojo.getValue())));
////            childQueryBuilder.should(queryBuilder.must(termQuery("data." + childAttr + ".keyword", repeatSearchFieldPojo.getValue())));
////            childQueryBuilder.should(likeQueryBuilder.must(termQuery("data." + childAttr, repeatSearchFieldPojo.getValue())));
//        }
        HasChildQueryBuilder hasChildQueryBuilder = JoinQueryBuilders.hasChildQuery(indexType, childQueryBuilder, ScoreMode.Max);
        return hasChildQueryBuilder;
    }

    /**
     * 根据表单ID以及查重规则进行高级搜索
     * @param aggDataFormIds
     * @param repeatSearchFieldPojos
     * @param seniorReCheckSearchDTO
     * @return
     * @throws XbbException
     */
    private XbbAggregatedPage<PaasFormDataEntityExt> getSeniorDuplicateData(List<Long> aggDataFormIds, List<RepeatSearchFieldPojo> repeatSearchFieldPojos, SeniorReCheckSearchDTO seniorReCheckSearchDTO) throws XbbException {
        String corpid = seniorReCheckSearchDTO.getCorpid();
        Integer businessType = seniorReCheckSearchDTO.getBusinessType();
        Long firstFormId = aggDataFormIds.get(0);
        IndexTypeEnum indexTypeEnum = indexTypeModel.getIndexType(corpid,businessType, SaasMarkEnum.SAAS.getCode());
        IndexTypeEnum indexTypeSubEnum = indexTypeModel.getSubIndexType(corpid, businessType, SaasMarkEnum.SAAS.getCode());


        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
        boolQueryBuilder.filter(termQuery("formId", firstFormId));
        if (Objects.equals(businessType, XbbRefTypeEnum.CONTACT.getCode()) || Objects.equals(businessType, XbbRefTypeEnum.SALES_OPPORTUNITY.getCode())) {
            boolQueryBuilder.filter(termsQuery("del", Arrays.asList(DelEnum.NORMAL.getDel(), DelEnum.APPROVE_STATUS.getDel())));
        } else {
            boolQueryBuilder.filter(termsQuery("del", Arrays.asList(DelEnum.NORMAL.getDel(), DelEnum.DELETE.getDel(), DelEnum.APPROVE_STATUS.getDel())));
        }
        if (seniorReCheckSearchDTO.getIsOpenWorkflow()) {
            // 如果开启了工作流，审批中数据会进入主表，查重的时候可以直接从主表查到
            boolQueryBuilder.filter(termsQuery(FieldTypeEnum.FLOW_STATUS.getAlias(), Arrays.asList(FlowStatusEnum.NOT_NEED_APPROVAL.getType(), FlowStatusEnum.PASS.getType(), FlowStatusEnum.APPROVAL_PENDING.getType(), FlowStatusEnum.IN_APPROVAL.getType())));
        } else {
            boolQueryBuilder.filter(termsQuery(FieldTypeEnum.FLOW_STATUS.getAlias(), Arrays.asList(FlowStatusEnum.NOT_NEED_APPROVAL.getType(), FlowStatusEnum.PASS.getType())));
        }
        repeatSearchFieldPojos.removeIf(item -> !Objects.equals(firstFormId, item.getFormId()));

        BoolQueryBuilder queryBuilder = boolQuery();
        for (RepeatSearchFieldPojo repeatSearchFieldPojo : repeatSearchFieldPojos) {
            //流水号字段特殊处理
            if (repeatSearchFieldPojo.getAttr().contains(StringConstant.POINT) && (Objects.equals(businessType,XbbRefTypeEnum.SALES_OPPORTUNITY.getCode()) || !Objects.equals(repeatSearchFieldPojo.getAttr(), StringConstant.PHONE))) {
                HasChildQueryBuilder hasChildQueryBuilder = subFormQueryBuilder(indexTypeSubEnum.getType(), corpid, repeatSearchFieldPojo);
                queryBuilder.should(hasChildQueryBuilder);
            } else {
                if (Objects.equals(RecheckMatchWayEnum.EXACT_MATCH.getCode(),repeatSearchFieldPojo.getMatchWay())) {
                    if (Objects.equals("serialNo",repeatSearchFieldPojo.getAttr())){
                        queryBuilder.should(termQuery(repeatSearchFieldPojo.getAttr()+ StringConstant.POINT + StringConstant.KEY_WORD, repeatSearchFieldPojo.getValue()));
                    } else {
                        //精确查询
                        queryBuilder.should(termQuery("data." + repeatSearchFieldPojo.getAttr() + StringConstant.POINT + StringConstant.KEY_WORD, repeatSearchFieldPojo.getValue()));
                    }
                } else {
                    //模糊查询
                    queryBuilder.should(matchPhraseQuery("data." + repeatSearchFieldPojo.getAttr(), repeatSearchFieldPojo.getValue()));
                }
            }
        }
        queryBuilder.minimumShouldMatch(MINI_MUM_SHOULD_MATCH);
        boolQueryBuilder.filter(queryBuilder);
        boolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD, indexTypeEnum.getType()));
        Integer page = 1;
        Integer pageSize = 10;
        PageRequest pageRequest = EsUtil.setPage(sourceBuilder, (page - 1), pageSize);
        sourceBuilder.query(boolQueryBuilder);
        SearchRequest searchRequest = new SearchRequest(indexTypeEnum.getIndex());
        searchRequest.source(sourceBuilder);
        XbbAggregatedPage<PaasFormDataEntityExt> esEntities = xbbElasticsearchRestTemplate.queryForPages(pageRequest, searchRequest, PaasFormDataEntityExt.class);
        return esEntities;
    }
}
